#include "mqtt_config.h"

mqtt_modbus_read_cmd_t mqtt_modbus_read_cmd[MQTT_MODBUS_READ_MAX];
mqtt_modbus_write_cmd_t mqtt_modbus_write_cmd[MQTT_MODBUS_WRITE_MAX];
mqtt_modbus_config_t mqtt_modbus_config[MQTT_MODBUS_CONFIG_MAX];

int mqtt_modbus_config_add(uint8_t  dev_addr, uint8_t  reg_type, uint16_t reg_addr, uint16_t flag, uint16_t tigger_greater, uint16_t tigger_equal, uint16_t tigger_less)
{
    int i = 0;
    mqtt_modbus_config_t *node = mqtt_modbus_config;

    for(i = 0; i < MQTT_MODBUS_CONFIG_MAX; i++){
        if(!node[i].used){
            node[i].used = 1;
            node[i].dev_addr = dev_addr;
            node[i].reg_type = reg_type;
            node[i].reg_addr = reg_addr;
            node[i].flag.v   = flag;
            node[i].tigger_greater = tigger_greater;
            node[i].tigger_equal = tigger_equal;
            node[i].tigger_less = tigger_less;
            return i;
        }
    }
    return -1;
}

int mqtt_modbus_config_delete(uint8_t  dev_addr, uint8_t  reg_type, uint16_t reg_addr)
{
    uint32_t node_addr = 0, input_addr = 0;
    mqtt_modbus_config_t *node = mqtt_modbus_config;

    input_addr = ((uint32_t)dev_addr << 24) + ((uint32_t)reg_type << 16) + (uint32_t)reg_addr;
    for(int i = 0; i < MQTT_MODBUS_CONFIG_MAX; i++){
        node_addr = ((uint32_t)node[i].dev_addr << 24) + ((uint32_t)node[i].reg_type << 16) + (uint32_t)node[i].reg_addr;
        if(node_addr == input_addr){
            memset(&node[i], 0, sizeof(mqtt_modbus_config_t));
            return i;
        }
    }
    return -1;
}

int mqtt_modbus_config_update(uint8_t  dev_addr, uint8_t  reg_type, uint16_t reg_addr, uint16_t reg_value)
{
    uint32_t node_addr = 0, input_addr = 0;
    mqtt_modbus_config_t *node = mqtt_modbus_config;

    input_addr = ((uint32_t)dev_addr << 24) + ((uint32_t)reg_type << 16) + (uint32_t)reg_addr;
    for(int i = 0; i < MQTT_MODBUS_CONFIG_MAX; i++){
        node_addr = ((uint32_t)node[i].dev_addr << 24) + ((uint32_t)node[i].reg_type << 16) + (uint32_t)node[i].reg_addr;
        if(node_addr == input_addr){
            if(reg_value != node[i].reg_value){
                node[i].reg_value = reg_value;
                if(node[i].flag.s.change){
                    node[i].sent = 0;
                }
                if(node[i].flag.s.greater){
                    if(reg_value > node[i].tigger_greater){
                        node[i].sent = 0;
                    }
                }
                if(node[i].flag.s.equal){
                    if(reg_value > node[i].tigger_equal){
                        node[i].sent = 0;
                    }
                }
                if(node[i].flag.s.less){
                    if(reg_value > node[i].tigger_less){
                        node[i].sent = 0;
                    }
                }
            }
            return i;
        }
    }
    return -1;
}


int mqtt_modbus_config_get_index(uint8_t  dev_addr, uint8_t  reg_type, uint16_t reg_addr)
{
    uint32_t node_addr = 0, input_addr = 0;
    mqtt_modbus_config_t *node = mqtt_modbus_config;

    input_addr = ((uint32_t)dev_addr << 24) + ((uint32_t)reg_type << 16) + (uint32_t)reg_addr;
    for(int i = 0; i < MQTT_MODBUS_CONFIG_MAX; i++){
        node_addr = ((uint32_t)node[i].dev_addr << 24) + ((uint32_t)node[i].reg_type << 16) + (uint32_t)node[i].reg_addr;
        if(node_addr == input_addr){
            return i;
        }
    }
    return -1;
}

int mqtt_modbus_config_print(uint8_t  dev_addr, uint8_t  reg_type, uint16_t reg_addr)
{
    uint32_t node_addr = 0, input_addr = 0;
    mqtt_modbus_config_t *node = mqtt_modbus_config;

    input_addr = ((uint32_t)dev_addr << 24) + ((uint32_t)reg_type << 16) + (uint32_t)reg_addr;
    for(int i = 0; i < MQTT_MODBUS_CONFIG_MAX; i++){
        node_addr = ((uint32_t)node[i].dev_addr << 24) + ((uint32_t)node[i].reg_type << 16) + (uint32_t)node[i].reg_addr;
        if(node_addr == input_addr){
            printf("[%d][%d][%d][%d][%d][%d][%d]\r\n",
                   node[i].dev_addr,
                   node[i].reg_type,
                   node[i].reg_addr,
                   node[i].reg_value,
                   node[i].tigger_greater,
                   node[i].tigger_equal,
                   node[i].tigger_less);
            return i;
        }
    }
    return -1;
}


int mqtt_genarate_modbus_cmds(void)
{
    int i = 0;
    mqtt_modbus_read_cmd_t vdata_modbus_cmd;
    mqtt_modbus_config_t *node = mqtt_modbus_config;

    memset(&vdata_modbus_cmd, 0xFF, sizeof(vdata_modbus_cmd));
    if(node){
        vdata_modbus_cmd.dev_addr = node->dev_addr;
        vdata_modbus_cmd.reg_type = node->reg_type;
        vdata_modbus_cmd.reg_addr = node->reg_addr;
        vdata_modbus_cmd.reg_num = 1;
    }
    for(i = 0; i < MQTT_MODBUS_CONFIG_MAX; i ++){
        if(!node[i].used){
            continue;
        }
        if(i != (MQTT_MODBUS_CONFIG_MAX - 1)){
            if((node[i].dev_addr != node[i + 1].dev_addr)||
               (node[i].reg_type != node[i + 1].reg_type)||
               ((node[i].reg_addr + 1) != node[i + 1].reg_addr)){
                printf("[%d][%d][%d][%d]\r\n", vdata_modbus_cmd.dev_addr, vdata_modbus_cmd.reg_type, vdata_modbus_cmd.reg_addr, vdata_modbus_cmd.reg_num);
                vdata_modbus_cmd.dev_addr = node[i + 1].dev_addr;
                vdata_modbus_cmd.reg_type = node[i + 1].reg_type;
                vdata_modbus_cmd.reg_addr = node[i + 1].reg_addr;
                vdata_modbus_cmd.reg_num = 1;
            }else{
                switch(vdata_modbus_cmd.reg_type){
                    case 1:
                        if(++vdata_modbus_cmd.reg_num >= MODBUS_READ_1X_MAX){
                            printf("[%d][%d][%d][%d]\r\n", vdata_modbus_cmd.dev_addr, vdata_modbus_cmd.reg_type, vdata_modbus_cmd.reg_addr, vdata_modbus_cmd.reg_num);
                            vdata_modbus_cmd.dev_addr = node[i].dev_addr;
                            vdata_modbus_cmd.reg_type = node[i].reg_type;
                            vdata_modbus_cmd.reg_addr = node[i].reg_addr;
                            vdata_modbus_cmd.reg_num = 1;
                        }
                        break;

                    case 2:
                        if(++vdata_modbus_cmd.reg_num >= MODBUS_READ_2X_MAX){
                            printf("[%d][%d][%d][%d]\r\n", vdata_modbus_cmd.dev_addr, vdata_modbus_cmd.reg_type, vdata_modbus_cmd.reg_addr, vdata_modbus_cmd.reg_num);
                            vdata_modbus_cmd.dev_addr = node[i].dev_addr;
                            vdata_modbus_cmd.reg_type = node[i].reg_type;
                            vdata_modbus_cmd.reg_addr = node[i].reg_addr;
                            vdata_modbus_cmd.reg_num = 1;
                        }
                        break;

                    case 3:
                        if(++vdata_modbus_cmd.reg_num >= MODBUS_READ_3X_MAX){
                            printf("[%d][%d][%d][%d]\r\n", vdata_modbus_cmd.dev_addr, vdata_modbus_cmd.reg_type, vdata_modbus_cmd.reg_addr, vdata_modbus_cmd.reg_num);
                            vdata_modbus_cmd.dev_addr = node[i].dev_addr;
                            vdata_modbus_cmd.reg_type = node[i].reg_type;
                            vdata_modbus_cmd.reg_addr = node[i].reg_addr;
                            vdata_modbus_cmd.reg_num = 1;
                        }
                        break;

                    case 4:
                        if(++vdata_modbus_cmd.reg_num >= MODBUS_READ_4X_MAX){
                            printf("[%d][%d][%d][%d]\r\n", vdata_modbus_cmd.dev_addr, vdata_modbus_cmd.reg_type, vdata_modbus_cmd.reg_addr, vdata_modbus_cmd.reg_num);
                            vdata_modbus_cmd.dev_addr = node[i].dev_addr;
                            vdata_modbus_cmd.reg_type = node[i].reg_type;
                            vdata_modbus_cmd.reg_addr = node[i].reg_addr;
                            vdata_modbus_cmd.reg_num = 1;
                        }
                        break;

                    default: break;
                }
            }
        }else{//最后一个
            printf("[%d][%d][%d][%d]\r\n", vdata_modbus_cmd.dev_addr, vdata_modbus_cmd.reg_type, vdata_modbus_cmd.reg_addr, vdata_modbus_cmd.reg_num);
            vdata_modbus_cmd.reg_num = 1;
        }
    }
    return 1;
}


int mqtt_modbus_read_config_file(void)
{
    char *a;
    char delims[] = ",";
    char *result = NULL;
    char line[100] = {0};
    int row = 0, col = 0;
    int value[8];
    FILE *fp = NULL;

    printf("mqtt_modbus_config size is -> %d\r\n", sizeof(mqtt_modbus_config));
    fp = fopen("mb_conf.csv","at+");
    if(fp != NULL){
        while (fgets(line, 100, fp)){
            a = line;
            col = 0;
            do{
                result = strtok( a, delims );
                a = NULL;
                switch(col){
                    case 0:break;
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:value[col] = atoi(result);break;
                    default:break;
                }
                col ++;
            }while(result != NULL);
            if(row >= 1){
                mqtt_modbus_config_add(value[1], value[2], value[3], value[4], value[5], value[6], value[7]);
            }
            row++;
        }
        fclose(fp);
        fp = NULL;
        mqtt_genarate_modbus_cmds();
        return 1;
    }
    return -1;
}
